<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>闭包的概念 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter05.html" />
    
    
    <link rel="prev" href="./chapter03.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="4"
        data-chapter-title="闭包的概念"
        data-filepath="chapter04.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 13:35:09 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        递归函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        匿名函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高阶函数
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        闭包的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        装饰器的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x95ED;&#x5305;">&#x95ED;&#x5305;</h1>
<p>&#x51FD;&#x6570;&#x53EA;&#x662F;&#x4E00;&#x6BB5;&#x53EF;&#x6267;&#x884C;&#x4EE3;&#x7801;&#xFF0C;&#x7F16;&#x8BD1;&#x540E;&#x5C31;&#x201C;&#x56FA;&#x5316;&#x201D;&#x4E86;&#xFF0C;&#x6BCF;&#x4E2A;&#x51FD;&#x6570;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x53EA;&#x6709;&#x4E00;&#x4EFD;&#x5B9E;&#x4F8B;&#xFF0C;&#x5F97;&#x5230;&#x51FD;&#x6570;&#x7684;&#x5165;&#x53E3;&#x70B9;&#x4FBF;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x51FD;&#x6570;&#x4E86;&#x3002;&#x51FD;&#x6570;&#x8FD8;&#x53EF;&#x4EE5;&#x5D4C;&#x5957;&#x5B9A;&#x4E49;&#xFF0C;&#x5373;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x6709;&#x4E86;&#x5D4C;&#x5957;&#x51FD;&#x6570;&#x8FD9;&#x79CD;&#x7ED3;&#x6784;&#xFF0C;&#x4FBF;&#x4F1A;&#x4EA7;&#x751F;&#x95ED;&#x5305;&#x95EE;&#x9898;&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x5D4C;&#x5957;">&#x51FD;&#x6570;&#x5D4C;&#x5957;</h2>
<p>&#x5728;&#x51FD;&#x6570;&#x91CC;&#x9762;&#x8FD8;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x5D4C;&#x5957;&#x591A;&#x5C42;&#xFF0C;&#x6267;&#x884C;&#x9700;&#x8981;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">outer</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&apos;outer----hello&apos;</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inner</span><span class="hljs-params">()</span>:</span>  <span class="hljs-comment"># inner&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x5728;outer&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x7684;</span>
        print(<span class="hljs-string">&apos;inner----hello&apos;</span>)
    inner()  <span class="hljs-comment"># inner&#x51FD;&#x6570;&#x53EA;&#x5728;outer&#x51FD;&#x6570;&#x5185;&#x90E8;&#x53EF;&#x89C1;</span>

outer()
<span class="hljs-comment"># inner()  &#x8FD9;&#x91CC;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x5728;outer&#x51FD;&#x6570;&#x5916;&#x90E8;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;&#x5230;inner&#x51FD;&#x6570;</span>
</code></pre>
<h2 id="&#x4EC0;&#x4E48;&#x662F;&#x95ED;&#x5305;">&#x4EC0;&#x4E48;&#x662F;&#x95ED;&#x5305;</h2>
<p>&#x95ED;&#x5305;&#x662F;&#x7531;&#x51FD;&#x6570;&#x53CA;&#x5176;&#x76F8;&#x5173;&#x7684;&#x5F15;&#x7528;&#x73AF;&#x5883;&#x7EC4;&#x5408;&#x800C;&#x6210;&#x7684;&#x5B9E;&#x4F53;(&#x5373;&#xFF1A;<strong>&#x95ED;&#x5305;=&#x51FD;&#x6570;&#x5757;+&#x5F15;&#x7528;&#x73AF;&#x5883;</strong>)&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">outer</span><span class="hljs-params">(n)</span>:</span>
    num = n
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inner</span><span class="hljs-params">()</span>:</span>
        <span class="hljs-keyword">return</span> num+<span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> inner

print(outer(<span class="hljs-number">3</span>)())  <span class="hljs-comment"># 4</span>
print(outer(<span class="hljs-number">5</span>)())  <span class="hljs-comment"># 5</span>
</code></pre>
<p>&#x5728;&#x8FD9;&#x6BB5;&#x7A0B;&#x5E8F;&#x4E2D;&#xFF0C;&#x51FD;&#x6570; inner &#x662F;&#x51FD;&#x6570; outer &#x7684;&#x5185;&#x5D4C;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14; inner &#x51FD;&#x6570;&#x662F;outer&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x6211;&#x4EEC;&#x6CE8;&#x610F;&#x5230;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;&#x5185;&#x5D4C;&#x51FD;&#x6570; inner &#x4E2D;&#x5F15;&#x7528;&#x5230;&#x5916;&#x5C42;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;num&#xFF0C;Python&#x89E3;&#x91CA;&#x5668;&#x4F1A;&#x8FD9;&#x4E48;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x5462;&#xFF1F;
&#x5148;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x8C03;&#x7528;&#x5206;&#x522B;&#x7531;&#x4E0D;&#x540C;&#x7684;&#x53C2;&#x6570;&#x8C03;&#x7528; outer &#x51FD;&#x6570;&#x5F97;&#x5230;&#x7684;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x5F97;&#x5230;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x9694;&#x79BB;&#x7684;(&#x76F8;&#x4E92;&#x4E0D;&#x5F71;&#x54CD;)&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x6BCF;&#x6B21;&#x8C03;&#x7528;outer&#x51FD;&#x6570;&#x540E;&#x90FD;&#x5C06;&#x751F;&#x6210;&#x5E76;&#x4FDD;&#x5B58;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;num,&#x8FD9;&#x91CC;outer&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7684;&#x5C31;&#x662F;&#x95ED;&#x5305;&#x3002;
<span style="color:red;font-weight:700">&#x5982;&#x679C;&#x5728;&#x4E00;&#x4E2A;&#x5185;&#x90E8;&#x51FD;&#x6570;&#x91CC;&#xFF0C;&#x5BF9;&#x5728;&#x5916;&#x90E8;&#x4F5C;&#x7528;&#x57DF;&#xFF08;&#x4F46;&#x4E0D;&#x662F;&#x5728;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#xFF09;&#x7684;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x5F15;&#x7528;&#xFF0C;&#x90A3;&#x4E48;&#x5185;&#x90E8;&#x51FD;&#x6570;&#x5C31;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x95ED;&#x5305;(closure).</span></p>
<h2 id="&#x4FEE;&#x6539;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x503C;">&#x4FEE;&#x6539;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x503C;</h2>
<p>&#x95ED;&#x5305;&#x91CC;&#x9ED8;&#x8BA4;&#x4E0D;&#x80FD;&#x4FEE;&#x6539;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">outer</span><span class="hljs-params">(n)</span>:</span>
    num = n
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inner</span><span class="hljs-params">()</span>:</span>
        num = num + <span class="hljs-number">1</span>
        <span class="hljs-keyword">return</span> num
    <span class="hljs-keyword">return</span> inner

print(outer(<span class="hljs-number">1</span>)())
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x62A5;&#x9519;&#xFF01;</p>
<p><code>UnboundLocalError: local variable &apos;num&apos; referenced before assignment</code></p>
<h3 id="&#x539F;&#x56E0;&#x5206;&#x6790;">&#x539F;&#x56E0;&#x5206;&#x6790;</h3>
<p>&#x5728;python&#x91CC;&#xFF0C;&#x53EA;&#x8981;&#x770B;&#x5230;&#x4E86;&#x8D4B;&#x503C;&#x8BED;&#x53E5;&#xFF0C;&#x5C31;&#x4F1A;&#x8BA4;&#x4E3A;&#x8D4B;&#x503C;&#x8BED;&#x53E5;&#x7684;&#x5DE6;&#x8FB9;&#x662F;&#x4E00;&#x4E2A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;<code>num = num + 1</code> &#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x91CC;&#xFF0C;<code>num</code> &#x5728;<code>=</code>&#x7684;&#x5DE6;&#x8FB9;&#xFF0C;python&#x89E3;&#x6790;&#x5668;&#x4F1A;&#x8BA4;&#x4E3A;&#x6211;&#x4EEC;&#x8981;&#x4FEE;&#x6539;<code>inner</code>&#x51FD;&#x6570;&#x91CC;<code>num</code>&#x8FD9;&#x4E2A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x662F;&#x672A;&#x58F0;&#x660E;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<h3 id="&#x89E3;&#x51B3;&#x65B9;&#x6848;">&#x89E3;&#x51B3;&#x65B9;&#x6848;</h3>
<p>&#x6211;&#x4EEC;&#x5206;&#x6790;&#x8FC7;&#xFF0C;&#x62A5;&#x9519;&#x7684;&#x539F;&#x56E0;&#x5728;&#x4E8E;&#x5F53;&#x6211;&#x4EEC;&#x5728;&#x95ED;&#x5305;&#x5185;&#x4FEE;&#x6539;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x4F1A;&#x88AB;python&#x89E3;&#x6790;&#x5668;&#x8BEF;&#x4F1A;&#x4E3A;&#x5185;&#x90E8;&#x51FD;&#x6570;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x5C31;&#x5728;&#x4E8E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x60F3;&#x529E;&#x6CD5;&#xFF0C;&#x8BA9;&#x89E3;&#x6790;&#x5668;&#x77E5;&#x9053;&#x6211;&#x4EEC;&#x4E0D;&#x662F;&#x8981;&#x4FEE;&#x6539;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x662F;&#x8981;&#x4FEE;&#x6539;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x3002;</p>
<ul>
<li>&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#xFF1A;&#x4F7F;&#x7528; nonlocal &#x5173;&#x952E;&#x5B57;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">outer</span><span class="hljs-params">(n)</span>:</span>
    num = n
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inner</span><span class="hljs-params">()</span>:</span>
        <span class="hljs-keyword">nonlocal</span> num  <span class="hljs-comment"># &#x4FEE;&#x6539;&#x524D;&#x4F7F;&#x7528;nonlocal&#x5173;&#x952E;&#x5B57;&#x5BF9; num &#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x8BF4;&#x660E;</span>
        num = num + <span class="hljs-number">1</span>
        <span class="hljs-keyword">return</span> num
    <span class="hljs-keyword">return</span> inner

print(outer(<span class="hljs-number">2</span>)())
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter03.html" class="navigation navigation-prev " aria-label="Previous page: 高阶函数"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter05.html" class="navigation navigation-next " aria-label="Next page: 装饰器的使用"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
